package com.kwin;

import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

public class TestTransaction {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.flushDB();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("hello","world");
        jsonObject.put("name", "java");

        //开启事务
        Transaction multi = jedis.multi();
        multi.watch("json");
        String result = jsonObject.toJSONString();

        try {
            multi.set("json", result);
            multi.set("json2", result);
//            int i = 100 / 0;
            multi.exec();
        } catch (Exception e) {
            e.printStackTrace();
            multi.discard();
        } finally {
            System.out.println(jedis.get("json"));
            System.out.println(jedis.get("json2"));
            multi.unwatch();
            jedis.close();
        }
    }
}
